package com.mycompany.myapp.web.rest;

import com.mycompany.myapp.service.AdvertisingService;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.service.dto.AdvertisingDTO;

import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.mycompany.myapp.domain.Advertising}.
 */
@RestController
@RequestMapping("/api")
public class AdvertisingResource {

    private final Logger log = LoggerFactory.getLogger(AdvertisingResource.class);

    private static final String ENTITY_NAME = "advertising";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final AdvertisingService advertisingService;

    public AdvertisingResource(AdvertisingService advertisingService) {
        this.advertisingService = advertisingService;
    }

    /**
     * {@code POST  /advertisings} : Create a new advertising.
     *
     * @param advertisingDTO the advertisingDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new advertisingDTO, or with status {@code 400 (Bad Request)} if the advertising has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/advertisings")
    public ResponseEntity<AdvertisingDTO> createAdvertising(@RequestBody AdvertisingDTO advertisingDTO) throws URISyntaxException {
        log.debug("REST request to save Advertising : {}", advertisingDTO);
        if (advertisingDTO.getId() != null) {
            throw new BadRequestAlertException("A new advertising cannot already have an ID", ENTITY_NAME, "idexists");
        }
        AdvertisingDTO result = advertisingService.save(advertisingDTO);
        return ResponseEntity.created(new URI("/api/advertisings/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /advertisings} : Updates an existing advertising.
     *
     * @param advertisingDTO the advertisingDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated advertisingDTO,
     * or with status {@code 400 (Bad Request)} if the advertisingDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the advertisingDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/advertisings")
    public ResponseEntity<AdvertisingDTO> updateAdvertising(@RequestBody AdvertisingDTO advertisingDTO) throws URISyntaxException {
        log.debug("REST request to update Advertising : {}", advertisingDTO);
        if (advertisingDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        AdvertisingDTO result = advertisingService.save(advertisingDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, advertisingDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /advertisings} : get all the advertisings.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of advertisings in body.
     */
    @GetMapping("/advertisings")
    public List<AdvertisingDTO> getAllAdvertisings() {
        log.debug("REST request to get all Advertisings");
        return advertisingService.findAll();
    }

    /**
     * {@code GET  /advertisings/:id} : get the "id" advertising.
     *
     * @param id the id of the advertisingDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the advertisingDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/advertisings/{id}")
    public ResponseEntity<AdvertisingDTO> getAdvertising(@PathVariable Long id) {
        log.debug("REST request to get Advertising : {}", id);
        Optional<AdvertisingDTO> advertisingDTO = advertisingService.findOne(id);
        return ResponseUtil.wrapOrNotFound(advertisingDTO);
    }

    /**
     * {@code DELETE  /advertisings/:id} : delete the "id" advertising.
     *
     * @param id the id of the advertisingDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/advertisings/{id}")
    public ResponseEntity<Void> deleteAdvertising(@PathVariable Long id) {
        log.debug("REST request to delete Advertising : {}", id);
        advertisingService.delete(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString())).build();
    }
}
